package com.heny.demo.strategy;



import com.heny.demo.entity.TurnPoint;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.heny.demo.entity.TurnPoint.createPoint;

public class TXTFileImportStrategy implements FileImportStrategy {
    @Override
    public List<TurnPoint> importData(File file) {
        List<TurnPoint> turnPoints = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split("\t");
                if (fields.length != 4) {
                    throw new IllegalArgumentException("Invalid line format: " + line);
                }

                // 解析字段
                String index = fields[0].trim(); // 坐标索引
                double x = Double.parseDouble(fields[1].trim()); // X 坐标
                double y = Double.parseDouble(fields[2].trim()); // Y 坐标
                double z = Double.parseDouble(fields[3].trim()); // Z 坐标

                // 创建 TurnPoint 实体
                TurnPoint turnPoint = new TurnPoint();
                turnPoint.setIndex(index);
                turnPoint.setCoordinate(createPoint(x, y, z)); // 使用 JTS Point 初始化
                turnPoints.add(turnPoint);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return turnPoints;
    }
}